package online.heycm.rbac.controller.basic;

import online.heycm.platform.common.entity.result.Optional;
import online.heycm.platform.common.entity.result.PageResult;
import online.heycm.platform.common.entity.result.Result;
import online.heycm.rbac.param.basic.DictionaryAddReq;
import online.heycm.rbac.param.basic.DictionaryEditReq;
import online.heycm.rbac.param.basic.DictionaryPageReq;
import online.heycm.rbac.param.basic.DictionaryRes;
import online.heycm.rbac.service.basic.DictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 基础服务 - 字典
 *
 * @author hey
 * @version 1.0
 * @date 2024/1/12 9:23
 */
@RestController
@RequestMapping("/basic/dictionary")
public class DictionaryController {

    @Autowired
    private DictionaryService dictionaryService;

    /**
     * 新增字典
     *
     * @param req
     * @return
     */
    @PostMapping("/add")
    public Result addDictionary(@RequestBody @Validated DictionaryAddReq req) {
        Optional optional = dictionaryService.addDictionary(req);
        return Result.optional(optional);
    }

    /**
     * 更新字典
     *
     * @param req
     * @return
     */
    @PostMapping("/edit")
    public Result editDictionary(@RequestBody @Validated DictionaryEditReq req) {
        Optional optional = dictionaryService.editDictionary(req);
        return Result.optional(optional);
    }

    /**
     * 查询字典详情
     *
     * @param dictId 字典id
     * @return
     */
    @GetMapping("/{dictId}")
    public Result<DictionaryRes> queryDetail(@PathVariable Integer dictId) {
        Optional<DictionaryRes> optional = dictionaryService.queryDetail(dictId);
        return Result.optional(optional);
    }

    /**
     * 字典分页
     *
     * @param req
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult<DictionaryRes>> pageDictionary(DictionaryPageReq req) {
        Optional<PageResult<DictionaryRes>> optional = dictionaryService.pageDictionary(req);
        return Result.optional(optional);
    }

    /**
     * 字典别名取列表
     *
     * @param dicAlias 字典别名
     * @return
     */
    @GetMapping("/list")
    public Result<List<DictionaryRes>> listDictionary(@RequestParam String dicAlias) {
        Optional<List<DictionaryRes>> optional = dictionaryService.listDictionary(dicAlias);
        return Result.optional(optional);
    }

}
